<template>
	<view class="bg-white">
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="content">草稿箱编辑</block>
				
		</cu-custom>
		<view style="padding: 10px;margin-top: 10px;">
			<view class="action" style="margin-bottom: 10px;">
				<text class="cuIcon-titles " style="color: #1E5EFF;"></text>
				<text style="color: #5A607F;font-size: 30rpx;">
					基础信息新增
				</text>
			</view>
			<view style="padding: 10px;">
				<!-- <view
					style="width: 100px;height: 97px;text-align: center;background-color: rgba(245, 246, 250, 1);padding: 10px;font-size: 14px;"
					@click="addImg">
					<view style="margin-top: 15px;">
						<image src="@/static/images/add.png" style="width: 20px;height: 20px;"></image>
					</view>
					<text style="display: inline-block;">
						添加照片
					</text>

				</view> -->

				<view class="cu-form-group">
					<view class="grid col-4 grid-square flex-sub">
						<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
							:data-url="imgList[index]">
							<image :src="imgList[index]" mode="aspectFill"></image>
							<view class="cu-tag bg-red radius" @tap.stop="DelImg" :data-index="index">
								<text class='cuIcon-close'></text>
							</view>
						</view>
						<!-- 	<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
							<text class='cuIcon-cameraadd'></text>
						</view> -->
						<view @tap="ChooseImage" v-if="imgList.length<1"
							style="text-align: center;background: #F5F6FA;border-radius:10px;">
							<text class='cuIcon-add'></text>
							<text
								style="display: inline-block;margin-top: 45px;color: #5A607F;font-size: 12px;font-weight: 500;">
								添加照片
							</text>
						</view>
					</view>
				</view>
				<!-- 	<view class="cu-form-group">
					<view class="grid col-4 grid-square flex-sub">
						<view @tap="ChooseImage" v-if="imgList.length<4"
							style="text-align: center;background: #F5F6FA;border-radius:10px;">
							<text class='cuIcon-add'></text>
							<text
								style="display: inline-block;margin-top: 45px;color: #5A607F;font-size: 12px;font-weight: 500;">
								添加照片
							</text>
						</view>
						<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
							style="border-radius:10px;" :data-url="imgList[index]">
							<image :src="imgList[index]" mode="aspectFill"></image>
							<view class="cu-tag addImgStyle" @tap.stop="DelImg" :data-index="index">
								<text class='cuIcon-delete' style="color: #FF3B3B;"></text>
							</view>
						</view>
					</view>
				</view> -->
				<view class="cu-form-group margin-top">
					<view class="title">读证时间</view>
					<picker mode="date" :value="createTime" @change="DateChangeCreateTime">
						<view class="picker">
							{{createTime}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group">
					<view class="title">姓名</view>
					<input name="input" v-model="dataObj.realname"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">性别</view>
					<!-- <input name="input" v-model="dataObj.sex"></input> -->
					<my-select range-key="text" value-key="value" dict-code="sex" v-model="dataObj.sex" />
				</view>
				<view class="cu-form-group">
					<view class="title">年龄</view>
					<!-- <input name="input" v-model="dataObj.age"></input> -->
					<input name="input" type="number" v-model="dataObj.age"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">民族</view>
					<input name="input" v-model="dataObj.nation"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">公民身份证号码</view>
					<input name="input" v-model="dataObj.idCard"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">出生日期</view>
					<!-- <input name="input" v-model="dataObj.birthday"></input> -->.
					<picker mode="date" :value="dataObj.birthday" @change="DateChange">
						<view class="picker">
							{{birthday}}
						</view>
					</picker>
					
					<!-- <picker mode="date" :value="dataObj.graduationTime"
						@change="DateChangeGraduationTime">
						<view class="picker">
							{{graduationTime}}
						</view>
					</picker> -->
					
					
				</view>
				<view class="cu-form-group">
					<view class="title">住址</view>
					<input name="input" v-model="dataObj.idCardAddress"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">签发机关</view>
					<input name="input" v-model="dataObj.idCardIssue"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">有效期限</view>
					<!-- <input name="input" v-model="dataObj.idCardValidPeriod"></input> -->
					<picker mode="date" :value="idCardValidPeriod" @change="DateChangeIdCardValidPeriod">
						<view class="picker">
							{{idCardValidPeriod.replace(/\-/g, "/")}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group">
					<view class="title">身份类别</view>
					<!-- <input name="input" v-model="dataObj.type"></input> -->
					<my-select range-key="text" value-key="value" dict-code="identity_category"
						v-model="dataObj.identityCategory" />
				</view>
				<view class="cu-form-group">
					<view class="title">公司</view>
					<!-- <input name="input" v-model="dataObj.company"></input> -->
					<my-select range-key="text" value-key="value" dict-code="crm_company,name,id"
						v-model="dataObj.company" />
				</view>
				<view class="cu-form-group">
					<view class="title">地址</view>
					<input disabled name="input" v-model="dataObj.companyAddress"></input>
				</view>
			</view>
			
			<view style="display: flex;margin-top: 15px;justify-content: space-between;align-items: center;">
				<view @click="submit"
					style="padding: 30rpx 110rpx; margin-left: 20rpx;margin-top: 20rpx; background-color: rgba(30, 94, 255, 1);border-radius: 30rpx;color: #FFFFFF;">
					提交审核
				</view>
				<view @click="save"
					style="padding: 30rpx 110rpx;margin-left: 20rpx;margin-top: 20rpx;border-radius: 30rpx;background-color: rgba(255, 173, 4, 1); color: #FFFFFF;">
					保存
				</view>
			</view>
		</view>
		<uni-popup ref="submitFlag" background-color="#fff" :mask-click="false">
			<view style="text-align: center;background-color: #FFFFFF;padding: 11rpx;width: 580rpx;height: 470rpx;border-radius: 42rpx;">
				<view>
					<view class="submit-item" >
						<image class="submit-icon" src="@/static/duihaoblue.png" />
					</view>
					<view style="margin-top: 28rpx;">
						<text class="submit-text">入职申请已经提交</text>
					</view>
					<view style="color: rgba(150, 150, 150, 1); margin-top: 23rpx;">
						<text>已提醒审核人员进行审核</text>
					</view>
					<button @click="sureOne" style="border-radius: 20rpx; background-color: rgba(69, 145, 254, 1);
					color: rgba(255, 255, 255, 1);width: 300rpx;margin-top: 27rpx;">确定</button>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="draftsFlag" background-color="#fff" :mask-click="false">
		<view>
			<view style="text-align: center;background-color: #FFFFFF;padding: 11rpx;width: 580rpx;height: 470rpx;border-radius: 42rpx;">
				<view class="submit-item" >
					<image class="submit-icon" src="@/static/images/draftIcon.png" />
				</view>
				<view style="margin-top: 28rpx;">
					<text class="submit-text">已保存进草稿箱</text>
				</view>
				<view style="color: rgba(150, 150, 150, 1); margin-top: 23rpx;">
					<text>您可以进入草稿箱查看</text>
				</view>
				<button @click="sureThree" style="border-radius: 20rpx; background-color: rgba(69, 145, 254, 1);
				color: rgba(255, 255, 255, 1);width: 300rpx;margin-top: 27rpx;">确定</button>
			</view>
		</view>
		</uni-popup>
	</view>
</template>

<script>
	import myDate from '@/components/my-componets/my-date.vue'
	import mySelect from '@/components/my-componets/my-select.vue'
	import {
		log
	} from '@/plugin/uni-simple-router/helpers/warn'
	export default {
		data() {
			return {
				index: -1,
				imgList: [],
				uploadUrl: "/sys/common/upload",
				createTime: '2000-01-01',
				birthday: '2000-01-01',
				idCardValidPeriod: '2000-01-01',
				submitFlag:false,
				draftsFlag:false,
				dataObj: {
					id: Math.floor(Math.random() * 10000000000000000000) + Date.now(),
					avatar: '',
					createTime: '2000-01-01',
					realname: '',
					sex: '',
					age: '',
					nation: '',
					idCard: '',
					birthday: '2000-01-01',
					idCardAddress: '',
					idCardIssue: '',
					idCardValidPeriod: '2000-01-01',
					identityCategory: '',
					company: '',
					companyAddress: '',
					approvalStatus: '1'
				}
			}
		},
		mounted() {

		},
		watch: {
			'dataObj.company': {
				immediate: true,
				handler(val, old) {
					if(val != ''){
						this.$http.get('/crm_company/crmCompany/list')
							.then(res => {
								let addressObj = res.data.result.records.filter((item)=>{
									return item.id == val
								})
								this.dataObj.companyAddress = addressObj[0].address
							}).catch(() => {
								this.$tip.loaded();
								this.$tip.toast('提交失败')
							});
					}
				},
			},
		},
		methods: {
			DateChangeIdCardValidPeriod(e) {
				this.idCardValidPeriod = e.detail.value
				this.dataObj.idCardValidPeriod = e.detail.value
			},
			DateChange(e) {
				this.birthday = e.detail.value
				this.dataObj.birthday = e.detail.value
			},
			DateChangeCreateTime(e) {
				this.createTime = e.detail.value
				this.dataObj.createTime = e.detail.value
			},

			ChooseImage() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log("1111111111111111111111")
						console.log(that.uploadUrl)
						console.log(that.$config.apiUrl)
						that.$http.upload(that.$config.apiUrl + that.uploadUrl, {
								filePath: res.tempFilePaths[0],
								name: 'file'
							})
							.then(res => {
								that.dataObj.avatar = res.data.message;
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						this.imgList = res.tempFilePaths
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			submit() {
				this.$refs.submitFlag.open('center')
				this.dataObj.approvalStatus = '2'
				// this.dataObj.createTime = this.dataObj.createTime + " 00:00:00"
				this.$http.post('/crm_pending_employment_application/crmPendingEmploymentApplication/edit', this.dataObj)
					.then(res => {
						if (res.data.success) {
							this.submitTrial()
						} else {
							if ('数据库中已存在该记录' == res.data.message) {
								this.submitTrial()
							} else {
								this.$tip.toast(res.data.message)
							}
						}
					}).catch(() => {
						this.$tip.loaded();
						this.$tip.toast('提交失败')
					});
			},
			save() {
				this.$refs.draftsFlag.open('center')
				this.dataObj.approvalStatus = '1'
				// this.dataObj.createTime = this.dataObj.createTime + " 00:00:00"
				this.$http.post('/crm_pending_employment_application/crmPendingEmploymentApplication/edit', this.dataObj)
					.then(res => {
						if (res.data.success) {
							this.$tip.toast('提交成功')
						} else {
							this.$tip.toast(res.data.message)
						}
					}).catch(() => {
						this.$tip.loaded();
						this.$tip.toast('提交失败')
					});
			},
			submitTrial() {
				// 提交审核
				this.$http.post(
						'/crm_pending_employment_application/crmPendingEmploymentApplication/submitTrial/' +
						this.dataObj.id)
					.then(res => {
						if (res.data.success) {
							this.$tip.toast('提交成功')
						} else {
							this.$tip.toast(res.data.message)
						}
					}).catch(() => {
						this.$tip.loaded();
						this.$tip.toast('提交失败')
					});
			},
			addImg() {

			},
			sureOne(){
				this.$refs.submitFlag.close()
				uni.navigateTo({
					url: '/pages/oa/crm_employment_pending/index'
				})
			},
			sureThree(){
				this.$refs.draftsFlag.open()
				uni.navigateTo({
					url: '/pages/oa/crm_employment_pending/drafts/index'
				})
			}
		}
	}
</script>

<style>
	.cu-form-group {
		background-color: #ffffff;
		padding: 0.5px 0;
		display: flex;
		align-items: center;
		min-height: 52px;
		justify-content: space-between;
	}

	.grid.grid-square>uni-view>uni-text[class*="cuIcon-"] {
		font-size: 27px;
		position: absolute;
		color: #8799a3;
		margin: auto;
		top: -10px;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}

	.addImgStyle {
		background: rgba(255, 255, 255, 0.63) !important;
		border-radius: 6px 0px 7px 0px;
		position: absolute;
		left: 0;
		width: 25px;
		height: 15px;
		font-size: 10px;
	}
	.submit-wrap {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 2100rpx;
		background-color: rgba(3, 3, 3, 0.28);
		z-index: 10000;
	}
	
	.submit-box {
		padding:11rpx;
		width: 580rpx;
		height: 470rpx;
		border-radius: 42rpx;
		position: absolute;
		right: 86rpx;
		background-color: #FFFFFF;
		margin-top: 888rpx;
		text-align: center;
	}
	
	.submit-item {
	
	
	}
	.submit-text{
	
		font-size: 32rpx;
	}
	
	.submit-icon {
		width: 118rpx;
		height: 118rpx;
		margin-top: 58rpx;
	}
</style>